﻿using Firestarter.DB.Schema.UI.RevEngDBWizard;
using FireStarterModeller.Test;
using NUnit.Framework;

namespace Firestarter.DB.Schema.Test.UI
{
    [TestFixture]
    public class TestFileExtensionFilterFormatter : TestBase
    {
        [Test]
        public void Test_Construct()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            FileExtensionsFilterFormatter formatter = new FileExtensionsFilterFormatter();

            //---------------Test Result -----------------------
            Assert.IsNotNull(formatter);
        }

        [Test]
        public void Test_AddFilterElement_WhenCalled_ShouldAdd()
        {
            //---------------Set up test pack-------------------
            FileExtensionsFilterFormatter formatter = CreateFileExtensionFilterFormatter();
            string extension = GetRandomString();
            string description = GetRandomString();
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, formatter.FileExtensionElements.Count); 
            //---------------Execute Test ----------------------            
            formatter.AddExtensionFilter(extension, description);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, formatter.FileExtensionElements.Count);
            Assert.AreEqual(extension, formatter.FileExtensionElements[0].Extension);
            Assert.AreEqual(description, formatter.FileExtensionElements[0].Description);
        }

        [Test]
        public void Test_AddFilterTwoElement_WhenCalled_ShouldAddBothElements()
        {
            //---------------Set up test pack-------------------
            FileExtensionsFilterFormatter formatter = CreateFileExtensionFilterFormatter();
            string extension = GetRandomString();
            string description = GetRandomString();
            string extension2 = GetRandomString();
            string description2 = GetRandomString();
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, formatter.FileExtensionElements.Count);
            //---------------Execute Test ----------------------            
            formatter.AddExtensionFilter(extension, description);
            formatter.AddExtensionFilter(extension2, description2);
            //---------------Test Result -----------------------
            Assert.AreEqual(2, formatter.FileExtensionElements.Count);
            Assert.AreEqual(extension, formatter.FileExtensionElements[0].Extension);
            Assert.AreEqual(description, formatter.FileExtensionElements[0].Description);
            Assert.AreEqual(extension2, formatter.FileExtensionElements[1].Extension);
            Assert.AreEqual(description2, formatter.FileExtensionElements[1].Description);
        }


        [Test]
        public void Test_AddFilterManyElement_WhenCalled_ShouldAddManyElements()
        {
            //---------------Set up test pack-------------------
            FileExtensionsFilterFormatter formatter = CreateFileExtensionFilterFormatter();
            string extension = GetRandomString();
            string description = GetRandomString();
            string extension2 = GetRandomString();
            string description2 = GetRandomString();
            string extension3 = GetRandomString();
            string description3 = GetRandomString();            //---------------Assert Precondition----------------
            Assert.AreEqual(0, formatter.FileExtensionElements.Count);
            //---------------Execute Test ----------------------            
            formatter.AddExtensionFilter(extension, description);
            formatter.AddExtensionFilter(extension2, description2);
            formatter.AddExtensionFilter(extension3, description3);
            //---------------Test Result -----------------------
            Assert.AreEqual(3, formatter.FileExtensionElements.Count);
            Assert.AreEqual(extension, formatter.FileExtensionElements[0].Extension);
            Assert.AreEqual(description, formatter.FileExtensionElements[0].Description);
            Assert.AreEqual(extension2, formatter.FileExtensionElements[1].Extension);
            Assert.AreEqual(description2, formatter.FileExtensionElements[1].Description);
            Assert.AreEqual(extension3, formatter.FileExtensionElements[2].Extension);
            Assert.AreEqual(description3, formatter.FileExtensionElements[2].Description);
        }

        [Test]
        public void Test_Format_WhenCalledWithNoFormattedElementsAndWithAllFileFilter_False_ShouldReturnEmptyExtensionFilter()
        {
            //---------------Set up test pack-------------------
            FileExtensionsFilterFormatter formatter = CreateFileExtensionFilterFormatter();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            formatter.Format(false);
            //---------------Test Result -----------------------
            Assert.IsEmpty(formatter.ExtensionFilter);
        }

        [Test]
        public void Test_Format_WhenCalledWithNoFormattedElements_ShouldReturnExtensionFilterForAllFiles()
        {
            //---------------Set up test pack-------------------
            FileExtensionsFilterFormatter formatter = CreateFileExtensionFilterFormatter();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            formatter.Format();
            //---------------Test Result -----------------------
            Assert.AreEqual("All Files (*.*)|*.*", formatter.ExtensionFilter);
        }


        [Test]
        public void Test_Format_WhenCalledWithOneFormattedElementAndWithAllFilesFilter_ShouldReturnExtensionFilterIncludingAllFiles()
        {
            //---------------Set up test pack-------------------
            FileExtensionsFilterFormatter formatter = CreateFileExtensionFilterFormatter();
            formatter.AddExtensionFilter("mdb","Microsoft Access");
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, formatter.FileExtensionElements.Count);
            //---------------Execute Test ----------------------
            formatter.Format();
            //---------------Test Result -----------------------
            Assert.AreEqual("Microsoft Access (*.mdb)|*.mdb|All Files (*.*)|*.*", formatter.ExtensionFilter);
        }


        [Test]
        public void Test_Format_WhenCalledWithOneFormattedElementAndNotWithAllFilesFilter_ShouldReturnExtensionFilter()
        {
            //---------------Set up test pack-------------------
            FileExtensionsFilterFormatter formatter = CreateFileExtensionFilterFormatter();
            const bool withAllFilesFilter = false;
            formatter.AddExtensionFilter("mdb", "Microsoft Access");
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, formatter.FileExtensionElements.Count);
            //---------------Execute Test ----------------------
            formatter.Format(withAllFilesFilter);
            //---------------Test Result -----------------------
            Assert.AreEqual("Microsoft Access (*.mdb)|*.mdb", formatter.ExtensionFilter);
        }

        [Test]
        public void Test_Format_WhenCalledWithTwoFormattedElementsAndWithAllFilesFilter_ShouldReturnExtensionFilterIncludingAllFiles()
        {
            //---------------Set up test pack-------------------
            FileExtensionsFilterFormatter formatter = CreateFileExtensionFilterFormatter();
            formatter.AddExtensionFilter("mdb", "Microsoft Access");
            formatter.AddExtensionFilter("txt", "Text Files");
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, formatter.FileExtensionElements.Count);
            //---------------Execute Test ----------------------
            formatter.Format();
            //---------------Test Result -----------------------
            Assert.AreEqual("Microsoft Access (*.mdb)|*.mdb|Text Files (*.txt)|*.txt|All Files (*.*)|*.*", formatter.ExtensionFilter);
        }

        [Test]
        public void Test_Format_WhenCalledWithTwoFormattedElementsAndNotWithAllFilesFilter_ShouldReturnExtensionFilter()
        {
            //---------------Set up test pack-------------------
            FileExtensionsFilterFormatter formatter = CreateFileExtensionFilterFormatter();
            const bool withAllFilesFilter = false;
            formatter.AddExtensionFilter("mdb", "Microsoft Access");
            formatter.AddExtensionFilter("txt", "Text Files");
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, formatter.FileExtensionElements.Count);
            //---------------Execute Test ----------------------
            formatter.Format(withAllFilesFilter);
            //---------------Test Result -----------------------
            Assert.AreEqual("Microsoft Access (*.mdb)|*.mdb|Text Files (*.txt)|*.txt", formatter.ExtensionFilter);
        }

        [Test]
        public void Test_Format_WhenCalledWithManyFormattedElementsAndWithAllFilesFilter_ShouldReturnExtensionFilterIncludingAllFiles()
        {
            //---------------Set up test pack-------------------
            FileExtensionsFilterFormatter formatter = CreateFileExtensionFilterFormatter();
            formatter.AddExtensionFilter("mdb", "Microsoft Access");
            formatter.AddExtensionFilter("accdb", "Microsoft Access 2007");
            formatter.AddExtensionFilter("txt", "Text Files");
            //---------------Assert Precondition----------------
            Assert.AreEqual(3, formatter.FileExtensionElements.Count);
            //---------------Execute Test ----------------------
            formatter.Format();
            //---------------Test Result -----------------------
            Assert.AreEqual("Microsoft Access (*.mdb)|*.mdb|Microsoft Access 2007 (*.accdb)|*.accdb|Text Files (*.txt)|*.txt|All Files (*.*)|*.*", formatter.ExtensionFilter);
        }

        [Test]
        public void Test_Format_WhenCalledWithManyFormattedElementsAndNotWithAllFilesFilter_ShouldReturnExtensionFilter()
        {
            //---------------Set up test pack-------------------
            FileExtensionsFilterFormatter formatter = CreateFileExtensionFilterFormatter();
            const bool withAllFilesFilter = false;
            formatter.AddExtensionFilter("mdb", "Microsoft Access");
            formatter.AddExtensionFilter("accdb", "Microsoft Access 2007");
            formatter.AddExtensionFilter("txt", "Text Files");
            //---------------Assert Precondition----------------
            Assert.AreEqual(3, formatter.FileExtensionElements.Count);
            //---------------Execute Test ----------------------
            formatter.Format(withAllFilesFilter);
            //---------------Test Result -----------------------
            Assert.AreEqual("Microsoft Access (*.mdb)|*.mdb|Microsoft Access 2007 (*.accdb)|*.accdb|Text Files (*.txt)|*.txt", formatter.ExtensionFilter);
        }

        private static string GetRandomString()
        {
            return TestUtilsShared.GetRandomString();
        }

        protected virtual FileExtensionsFilterFormatter CreateFileExtensionFilterFormatter()
        {
            return new FileExtensionsFilterFormatter();
        }
    }
}